package com.example.app.api;

import com.example.app.interfaces.ResponseCallBack;
import com.example.app.viewmodel.Goods;
import com.example.app.viewmodel.ResponseData;
import com.example.app.viewmodel.cart.Cart;
import com.example.app.viewmodel.catalog.Catalog;
import com.example.app.viewmodel.detail.Detail;
import com.example.app.viewmodel.detail.Info;
import com.example.app.viewmodel.home.Home;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;


public class Apiservice {
    private static final String baseUrl = "http://hmapp.net:8888";

    public static void GetHome(ResponseCallBack<ResponseData<Home>> responseCallBack){
        OkGo.<String>get(baseUrl + "/wx/home/index").execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                ResponseData<Home> rh = new Gson().fromJson(response.body(),new TypeToken<ResponseData<Home>>(){}.getType());
                responseCallBack.Success(rh);
            }
        });
    }

    public static void GetCatalog(ResponseCallBack<ResponseData<Catalog>> responseCallBack){
        OkGo.<String>get(baseUrl + "/wx/catalog/index").execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                ResponseData<Catalog> rc = new Gson().fromJson(response.body(),new TypeToken<ResponseData<Catalog>>(){}.getType());
                responseCallBack.Success(rc);
            }
        });
    }

    public static void GetCatalogCurrent(ResponseCallBack<ResponseData<Catalog>> responseCallBack, int id){
        OkGo.<String>get(baseUrl + "/wx/catalog/current?id=" + id).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                ResponseData<Catalog> rc = new Gson().fromJson(response.body(),new TypeToken<ResponseData<Catalog>>(){}.getType());
                responseCallBack.Success(rc);
            }
        });
    }

    public static void GetGoods(ResponseCallBack<Goods<Detail>> responseCallBack, int id){
        OkGo.<String>get(baseUrl + "/wx/goods/detail?id=" + id).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                Goods<Detail> gd = new Gson().fromJson(response.body(),new TypeToken<Goods<Detail>>(){}.getType());
                responseCallBack.Success(gd);
            }
        });
    }

    public static void GetCart(ResponseCallBack<ResponseData<Cart>> responseCallBack){
        OkGo.<String>get(baseUrl + "/wx/cart/index").execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                ResponseData<Cart> rc = new Gson().fromJson(response.body(),new TypeToken<ResponseData<Cart>>(){}.getType());
                responseCallBack.Success(rc);
            }
        });
    }
}
